﻿


Namespace FL_8x0.Common

    Module mdlDevice

        Public uLPU As New ModbusServer     'LPU控制句柄



        '=====================================================================
        '函 数 名: StartServer
        '参    数: 
        '返 回 值: MODBUS_OK / MODBUS_ERR
        '功能描述: 启动LPU通讯
        '=====================================================================
        Public Function StartServer() As UShort
            If IsNothing(uLPU) Then
                uLPU = New ModbusServer
            End If

            '之前已经打开时先关闭
            If uLPU.IsServerRunning Then
                Return ModbusServer.MODBUS_ERR
            End If

            '打开
            If uLPU.StartServer(g_strStartupPath & "\JSON\modbus.json") <> ModbusServer.MODBUS_OK Then
                log.WriteLogError("设备连接", "设备连接错误！")
                Return ModbusServer.MODBUS_ERR
            End If

            Return ModbusServer.MODBUS_OK
        End Function



        '=====================================================================
        '函 数 名: StopServer
        '参    数: 
        '返 回 值: MODBUS_OK / MODBUS_ERR
        '功能描述: 停止LPU通讯
        '=====================================================================
        Public Function StopServer() As UShort
            If Not IsNothing(uLPU) Then
                If uLPU.IsServerRunning Then
                    If uLPU.StopServer() <> ModbusServer.MODBUS_OK Then
                        Return ModbusServer.MODBUS_ERR
                    End If
                End If
            End If

            Return ModbusServer.MODBUS_OK
        End Function



        '=====================================================================
        '函 数 名: ReadInput
        '参    数: uiIndex = 索引（0-15）
        '返 回 值: True / False
        '功能描述: 读IO输入
        '=====================================================================
        Public Function ReadInput(uiIndex As UInt16) As Boolean
            If uiIndex < 16 Then
                If Not IsNothing(uLPU) Then
                    If uLPU.IsServerRunning Then
                        If uLPU.IsModbusOK() = True Then '正常工作
                            Dim id As Byte = 1

                            Dim tmp As UInt16 = uLPU.IN_ReadUint16(id, ADDR_IN_IO)
                            Dim mask As UInt16 = &H1 << uiIndex

                            Return IIf((tmp And mask) <> 0, True, False)
                        End If
                    End If
                End If
            End If

            Return False
        End Function


        '=====================================================================
        '函 数 名: ReadOutput
        '参    数: uiIndex = 索引（0-15）
        '返 回 值: True / False
        '功能描述: 读IO输出
        '=====================================================================
        Public Function ReadOutput(uiIndex As UInt16) As Boolean
            If uiIndex < 16 Then
                If Not IsNothing(uLPU) Then
                    If uLPU.IsServerRunning Then
                        If uLPU.IsModbusOK() = True Then '正常工作
                            Dim id As Byte = 1

                            Dim tmp As UInt16 = uLPU.OUT_ReadUint16(id, ADDR_OUT_IO)
                            Dim mask As UInt16 = &H1 << (2 + uiIndex)

                            Return IIf((tmp And mask) <> 0, True, False)
                        End If
                    End If
                End If
            End If

            Return False
        End Function


        '=====================================================================
        '函 数 名: ReadOutputBuffer
        '参    数: uiIndex = 索引（0-15）
        '返 回 值: True / False
        '功能描述: 读IO输出缓冲
        '=====================================================================
        Public Function ReadOutputBuffer(uiIndex As UInt16) As Boolean
            If uiIndex < 16 Then
                If Not IsNothing(uLPU) Then
                    If uLPU.IsServerRunning Then
                        If uLPU.IsModbusOK() = True Then '正常工作
                            Dim id As Byte = 1

                            Dim tmp As UInt16 = uLPU.OUT_ReadBufferUint16(id, ADDR_OUT_IO)
                            Dim mask As UInt16 = &H1 << (2 + uiIndex)

                            Return IIf((tmp And mask) <> 0, True, False)
                        End If
                    End If
                End If
            End If

            Return False
        End Function



        '=====================================================================
        '函 数 名: WriteOutput
        '参    数: uiIndex = 索引（0-15）
        '          blnStates = True / False
        '返 回 值: 
        '功能描述: 写IO输出
        '=====================================================================
        Public Sub WriteOutput(uiIndex As UInt16, blnStates As Boolean)
            If uiIndex < 16 Then
                If Not IsNothing(uLPU) Then
                    If uLPU.IsServerRunning Then
                        If uLPU.IsModbusOK() = True Then '正常工作
                            Dim id As Byte = 1

                            Dim tmp As UInt16 = uLPU.OUT_ReadBufferUint16(id, ADDR_OUT_IO)
                            Dim mask As UInt16 = &H1 << (2 + uiIndex)

                            If blnStates = True Then
                                tmp = tmp Or mask
                            Else
                                tmp = tmp And (Not mask)
                            End If

                            uLPU.OUT_WriteUint16(id, ADDR_OUT_IO, tmp)
                        End If
                    End If
                End If
            End If

            Return
        End Sub

    End Module
End Namespace